package com.dianrui.moonfire.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.moonfire.R;
import com.dianrui.moonfire.activity.WebViewActivity;
import com.dianrui.moonfire.bean.HelpList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HelpListAdapter extends BaseAdapter {

    List<HelpList> list;
    Context context;


    public HelpListAdapter(Context context, List<HelpList> list) {
        this.list = list;
        this.context = context;

    }

    public void refresh(List<HelpList> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_help_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            final HelpList helpList = list.get(position);
            holder.helpName.setText(helpList.title);
            holder.help_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.startActivity(new Intent(context, WebViewActivity.class).putExtra("type", "HelpArticle").putExtra("article_id",helpList.article_id));

                }
            });
        }
        return convertView;
    }


     class ViewHolder {

        @BindView(R.id.help_name)
        TextView helpName;
         @BindView(R.id.help_layout)
        LinearLayout help_layout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
